using IcsSEMCtrl.Com.Secs;
using IcsSEMCtrl.Db;
using IcsSEMCtrl.Global.Device;
using IcsSEMCtrl.Service.Alarm;
using IcsSEMCtrl.Service.Logger;
using IcsSEMCtrl.Service.Material;
using IcsSEMCtrl.Util.Client;
using Lifter.Device;
using Lifter.Device.Dispatcher;
using Lifter.Device.Service.Alarm;
using Lifter.Device.Unit;
using Lifter.Global;
using Lifter.Global.Device;
using SEComEnabler.SEComPlugIn;
using SEComEnabler.SEComStructure;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using System.Xml;
using TKT.Lifter.CIM.Lifter.Device.Unit;
using TKT.Lifter.CIM.Lifter.HSMSMsg.HSMSConstant;
using TKT.Lifter.CIM.Lifter.HSMSMsg.TransferActionMsg;

namespace Lifter.Com.Secom
{
    public class SecomDriver : AbstractSecsDriver
    {
        private CSEComPlugIn secsDriver = null;
        private SEComError.SEComPlugIn errorHandler = SEComError.SEComPlugIn.ERR_NONE;
        private string driverName = "LFTC";
        public LifterSEM LftSem = (LifterSEM)Naming.GetDeviceManager().GetDevice();
        private string[] ceidList = new string[999];
        private Dictionary<int, MsgCEID> msgCeidDict = new Dictionary<int, MsgCEID>();//所有CEID字典表
        private string[] reportList = new string[999];
        private Dictionary<int, MsgVID> msgVidDict = new Dictionary<int, MsgVID>();//加载所有的VID字典表
        private string[] vidList = new string[999];
        private string[] vidMapper = new string[999];
        public override event ReceivedMessage OnReceiveMessage;
        public override event UnknownMessage OnUnknownMessage;
        public override event TimeoutOccured OnTimeoutOccured;
        public override event ReceivedInvalidMessage OnInvalidMessage;
        public override event Connected OnConnected;
        public override event Disconnected OnDisconnected;
        public override event AbortMessage OnAbortMessage;
        public override event Secs1Log OnSecs1Log;
        public override event Secs2Log OnSecs2Log;
        #region "ReceiveMessage"
        private void ReceiveS1F1(SEComData secsData)
        {
            SXTransaction sXTransaction = new SXTransaction();
            SecsValue secsValue = new SecsValue(secsData);
            if (this.OnReceiveMessage != null)
            {
                this.OnReceiveMessage(this, new SecsEventArgs("1", "1", secsValue, sXTransaction));
            }
        }
        private void ReceiveS1F3(SEComData secsData)
        {
            int itemCount = 0;
            string[] dataItemArr = new string[1];
            try
            {
                itemCount = secsData.DataItems[0].Count;
                dataItemArr = new string[itemCount];
                for (int i = 0; i < itemCount; i++)
                {
                    dataItemArr[i] = secsData.DataItems[0][i].Value.ToString();
                }
            }
            catch (ArgumentOutOfRangeException)
            {
            }
            SXTransaction sXTransaction = new SXTransaction();
            SecsValue secsValue = new SecsValue(secsData);
            for (int i = 0; i < itemCount; i++)
            {
                string key = string.Format("SVID{0}", i + 1);
                secsValue.AddValue(key, dataItemArr[i]);
                secsValue.AddRequestValue(key);
            }
            if (this.OnReceiveMessage != null)
            {
                this.OnReceiveMessage(this, new SecsEventArgs("1", "3", secsValue, sXTransaction));
            }
        }
        private void ReceiveS1F13(SEComData secsData)
        {
            SecsValue secsValue = new SecsValue(secsData);
            SXTransaction sXTransaction = new SXTransaction();
            if (this.OnReceiveMessage != null)
            {
                this.OnReceiveMessage(this, new SecsEventArgs("1", "13", secsValue, sXTransaction));
            }
        }
        private void ReceiveS1F15(SEComData secsData)
        {
            SXTransaction sXTransaction = new SXTransaction();
            SecsValue secsValue = new SecsValue(secsData);
            if (this.OnReceiveMessage != null)
            {
                this.OnReceiveMessage(this, new SecsEventArgs("1", "15", secsValue, sXTransaction));
            }
        }
        private void ReceiveS1F17(SEComData secsData)
        {
            SXTransaction sXTransaction = new SXTransaction();
            SecsValue secsValue = new SecsValue(secsData);
            if (this.OnReceiveMessage != null)
            {
                this.OnReceiveMessage(this, new SecsEventArgs("1", "17", secsValue, sXTransaction));
            }
        }
        private void ReceiveS2F13(SEComData secsData)
        {
            int itemCount = 0;
            string[] dataItemArr = new string[1];
            try
            {
                itemCount = secsData.DataItems[0].Count;
                dataItemArr = new string[itemCount];
                for (int i = 0; i < itemCount; i++)
                {
                    dataItemArr[i] = secsData.DataItems[0][i].Value.ToString();
                }
            }
            catch (ArgumentOutOfRangeException)
            {
            }
            SXTransaction sXTransaction = new SXTransaction();
            sXTransaction.Stream = 2;
            sXTransaction.Function = 14;
            sXTransaction.Wait = false;
            sXTransaction.MessageData = "Equipment Constant Request (ECR)";
            SecsValue secsValue = new SecsValue(secsData);
            secsValue.AddValue("ECIDCOUNT", itemCount.ToString());
            for (int i = 0; i < itemCount; i++)
            {
                secsValue.AddValue(i.ToString(), dataItemArr[i]);
            }
            if (this.OnReceiveMessage != null)
            {
                this.OnReceiveMessage(this, new SecsEventArgs("2", "13", secsValue, sXTransaction));
            }
        }
        private void ReceiveS10F3(SEComData secsData)
        {
            string terminalId = "";
            string terminalMsg = "";
            try
            {
                terminalId = secsData.DataItems[0][0].Value.ToString();
                terminalMsg = secsData.DataItems[0][1].Value.ToString();
            }
            catch (ArgumentOutOfRangeException)
            {
            }
            SXTransaction sXTransaction = new SXTransaction();
            sXTransaction.Stream = 10;
            sXTransaction.Function = 4;
            sXTransaction.Wait = false;
            sXTransaction.MessageData = "Reply Terminal Display.";
            SecsValue secsValue = new SecsValue(secsData);
            secsValue.AddValue("TERMINALID", terminalId);
            secsValue.AddValue("TERMINALMESSAGE", terminalMsg);
            secsValue.AddRequestValue("ACK");
            if (this.OnReceiveMessage != null)
            {
                this.OnReceiveMessage(this, new SecsEventArgs("10", "3", secsValue, sXTransaction));
            }
        }
        private void ReceiveS6F15(SEComData secsData)
        {
            string sCEID = "";
            try
            {
                sCEID = secsData.DataItems[0][0].Value.ToString();
            }
            catch (ArgumentOutOfRangeException)
            {
            }
            SXTransaction sXTransaction = new SXTransaction();
            sXTransaction.Stream = 6;
            sXTransaction.Function = 16;
            sXTransaction.Wait = false;
            sXTransaction.MessageData = "Reply Event Report Request";
            SecsValue secsValue = new SecsValue(secsData);
            secsValue.AddValue("CEID", sCEID);
            secsValue.AddRequestValue("DATAID");
            secsValue.AddRequestValue("CEID");
            secsValue.AddRequestValue("REPORTID");
            secsValue.AddRequestValue("VALUE");
            if (this.OnReceiveMessage != null)
            {
                this.OnReceiveMessage(this, new SecsEventArgs("6", "15", secsValue, sXTransaction));
            }
        }
        private void ReceiveS5F5(SEComData secsData)
        {
            string alarmId = "";
            try
            {
                alarmId = secsData.DataItems[0].Value.ToString();
            }
            catch (ArgumentOutOfRangeException)
            {
            }
            SXTransaction sXTransaction = new SXTransaction();
            sXTransaction.Stream = 5;
            sXTransaction.Function = 6;
            sXTransaction.Wait = false;
            sXTransaction.MessageData = "Reply List Alarms Request";
            SecsValue secsValue = new SecsValue(secsData);
            secsValue.AddValue("ALARMID", alarmId);
            secsValue.AddRequestValue("ALARMCODE");
            secsValue.AddRequestValue("ALARMID");
            secsValue.AddRequestValue("ALARMTEXT");
            if (this.OnReceiveMessage != null)
            {
                this.OnReceiveMessage(this, new SecsEventArgs("5", "5", secsValue, sXTransaction));
            }
        }
        private void ReceiveS5F3(SEComData secsData)
        {
            string alarmCode = "";
            string alarmId = "";
            try
            {
                alarmCode = secsData.DataItems[0][0].Value.ToString();
                alarmId = secsData.DataItems[0][1].Value.ToString();
            }
            catch (ArgumentOutOfRangeException)
            {
            }
            SXTransaction sXTransaction = new SXTransaction();
            sXTransaction.Stream = 5;
            sXTransaction.Function = 4;
            sXTransaction.Wait = false;
            sXTransaction.MessageData = "Reply Enable or Disable Alarm Send";
            SecsValue secsValue = new SecsValue(secsData);
            secsValue.AddValue("ALARMID", alarmId);
            secsValue.AddValue("ALARMCODE", alarmCode);
            secsValue.AddRequestValue("ALARMCODE");
            secsValue.AddRequestValue("ALARMID");
            if (this.OnReceiveMessage != null)
            {
                this.OnReceiveMessage(this, new SecsEventArgs("5", "3", secsValue, sXTransaction));
            }
        }
        private void ReceiveS2F49(SEComData secsData)
        {
            SXTransaction sXTransaction = new SXTransaction();

            SecsValue secsValue = new SecsValue(secsData);

            if (this.OnReceiveMessage != null)
            {
                this.OnReceiveMessage(this, new SecsEventArgs("2", "49", secsValue, sXTransaction));
            }
        }
        private void ReceiveS2F41(SEComData secsData)
        {
            SXTransaction sXTransaction = new SXTransaction();
            SecsValue secsValue = new SecsValue(secsData);
            if (this.OnReceiveMessage != null)
            {
                this.OnReceiveMessage(this, new SecsEventArgs("2", "41", secsValue, sXTransaction));
            }
        }
        private void ReceiveS2F33(SEComData secsData)
        {
            string value = "";
            int num = 0;
            try
            {
                value = secsData.DataItems[0][0].Value.ToString();
                num = int.Parse(secsData.DataItems[0][1].Value.ToString());
            }
            catch (ArgumentOutOfRangeException)
            {
            }
            SXTransaction sXTransaction = new SXTransaction();
            sXTransaction.Stream = 2;
            sXTransaction.Function = 34;
            sXTransaction.Wait = false;
            sXTransaction.MessageData = "Reply Define Report";
            SecsValue secsValue = new SecsValue(secsData);
            secsValue.AddValue("DATAID", value);
            secsValue.AddValue("REPORTCOUNT", num.ToString());
            if (num != 0)
            {
                string str = "C:\\ecp\\Lifter\\INI\\";
                if (File.Exists(Application.StartupPath + "\\ini\\RPT.ini"))
                {
                    str = Application.StartupPath + "\\ini\\";
                }
                File.SetAttributes(str + "RPT.ini", FileAttributes.Normal);
                File.Delete(str + "RPT.ini");
                File.Create(str + "RPT.ini").Close();
                using (TextWriter textWriter = new StreamWriter(str + "RPT.ini"))
                {
                    textWriter.WriteLine("[RPTID]");
                    for (int i = 0; i < num; i++)
                    {
                        string str2 = secsData.DataItems[0][1][i][0].Value.ToString();
                        int num2 = int.Parse(secsData.DataItems[0][1][i][1].Value.ToString());
                        string text = "";
                        for (int j = 0; j < num2; j++)
                        {
                            text = text + secsData.DataItems[0][1][i][1][j].Value.ToString() + ",";
                        }
                        textWriter.WriteLine(str2 + "=" + text);
                    }
                }
            }
            secsValue.AddRequestValue("ACK");
            if (this.OnReceiveMessage != null)
            {
                this.OnReceiveMessage(this, new SecsEventArgs("2", "33", secsValue, sXTransaction));
            }
        }
        private void ReceiveS2F31(SEComData secsData)
        {
            SecsValue secsValue = new SecsValue(secsData);

            SXTransaction sXTransaction = new SXTransaction();

            if (this.OnReceiveMessage != null)
            {
                this.OnReceiveMessage(this, new SecsEventArgs("2", "31", secsValue, sXTransaction));
            }
        }
        private void ReceiveS2F15(SEComData secsData)
        {
            int num = 0;
            string[] array = new string[1];
            string[] array2 = new string[1];
            try
            {
                num = secsData.DataItems[0].Count;
                array = new string[num];
                array2 = new string[num];
                for (int i = 0; i < num; i++)
                {
                    array[i] = secsData.DataItems[0][i][0].Value.ToString();
                    array2[i] = secsData.DataItems[0][i][1].Value.ToString();
                }
            }
            catch (ArgumentOutOfRangeException)
            {
            }
            SXTransaction sXTransaction = new SXTransaction();
            sXTransaction.Stream = 2;
            sXTransaction.Function = 16;
            sXTransaction.Wait = false;
            sXTransaction.MessageData = "New Equipment Constant Send(ECS)";
            SecsValue secsValue = new SecsValue(secsData);
            secsValue.AddValue("ECIDCOUNT", num.ToString());
            for (int i = 0; i < num; i++)
            {
                secsValue.AddValue(i.ToString(), array[i].ToString() + "," + array2[i].ToString());
                secsValue.AddRequestValue(i.ToString());
            }
            if (this.OnReceiveMessage != null)
            {
                this.OnReceiveMessage(this, new SecsEventArgs("2", "15", secsValue, sXTransaction));
            }
        }
        private void ReceiveS2F17(SEComData secsData)
        {
            SXTransaction sXTransaction = new SXTransaction();
            sXTransaction.Stream = 2;
            sXTransaction.Function = 18;
            sXTransaction.Wait = false;
            sXTransaction.MessageData = "Reply Date and Time Set Request";
            SecsValue secsValue = new SecsValue(secsData);
            secsValue.AddRequestValue("DATETIME");
            if (this.OnReceiveMessage != null)
            {
                this.OnReceiveMessage(this, new SecsEventArgs("2", "17", secsValue, sXTransaction));
            }
        }
        private void ReceiveS2F18(SEComData secsData)
        {
            SXTransaction sXTransaction = new SXTransaction();
            sXTransaction.Stream = 2;
            sXTransaction.Function = 18;
            sXTransaction.Wait = false;
            sXTransaction.MessageData = "Reply Date and Time Set Request";
            string value = secsData.DataItems[0].Value.ToString();
            SecsValue secsValue = new SecsValue(secsData);
            secsValue.AddValue("DATETIME", value);
            if (this.OnReceiveMessage != null)
            {
                this.OnReceiveMessage(this, new SecsEventArgs("2", "18", secsValue, sXTransaction));
            }
        }
        private void ReceiveUnknowMessage(SEComData secsData)
        {
        }
        #endregion

        #region "ReplyMessage"
        private void ReplyS1F1(SXTransaction transaction, SecsValue secsValue)
        {
            SEComData sEComData = (SEComData)secsValue.SecsData;
            this.errorHandler = this.secsDriver.Reply(this.driverName, transaction, sEComData.HeaderItems.SystemBytes);
            if (this.errorHandler != SEComError.SEComPlugIn.ERR_NONE)
            {
                string text = this.errorHandler.ToString();
            }
        }
        private void ReplyS1F13(SXTransaction transaction, SecsValue secsValue)
        {
            SEComData sEComData = (SEComData)secsValue.SecsData;
            this.errorHandler = this.secsDriver.Reply(this.driverName, transaction, sEComData.HeaderItems.SystemBytes);
            if (this.errorHandler != SEComError.SEComPlugIn.ERR_NONE)
            {
                string text = this.errorHandler.ToString();
            }
        }
        private void ReplyUnknowMessage(SXTransaction transaction, SecsValue secsValue)
        {
            SEComData sEComData = (SEComData)secsValue.SecsData;
            this.errorHandler = this.secsDriver.Reply(this.driverName, transaction, sEComData.HeaderItems.SystemBytes);
            if (this.errorHandler != SEComError.SEComPlugIn.ERR_NONE)
            {
                string text = this.errorHandler.ToString();
            }
        }
        private void ReplyS10F3(SXTransaction transaction, SecsValue secsValue)
        {
            SEComData sEComData = (SEComData)secsValue.SecsData;
            transaction.WriteNode(SX.SECSFormat.B, 1, secsValue.GetRequestValue("ACK"), "ACK", new int[0]);
            this.errorHandler = this.secsDriver.Reply(this.driverName, transaction, sEComData.HeaderItems.SystemBytes);
            if (this.errorHandler != SEComError.SEComPlugIn.ERR_NONE)
            {
                string text = this.errorHandler.ToString();
            }
        }
        private void ReplyS6F15(SXTransaction transaction, SecsValue secsValue)
        {
            SEComData sEComData = (SEComData)secsValue.SecsData;
            transaction.WriteNode(SX.SECSFormat.L, 3, "", "", new int[0]);
            transaction.WriteNode(SX.SECSFormat.U4, 1, secsValue.GetRequestValue("DATAID"), "DATAID", new int[0]);
            transaction.WriteNode(SX.SECSFormat.U2, 1, secsValue.GetRequestValue("CEID"), "CEID", new int[0]);
            transaction.WriteNode(SX.SECSFormat.L, 1, "", "", new int[0]);
            transaction.WriteNode(SX.SECSFormat.L, 2, "", "", new int[0]);
            transaction.WriteNode(SX.SECSFormat.U2, 1, secsValue.GetRequestValue("REPORTID"), "ReportID", new int[0]);
            transaction.WriteNode(SX.SECSFormat.L, 1, "", "", new int[0]);
            transaction.WriteNode(SX.SECSFormat.A, 1, secsValue.GetRequestValue("VALUE"), "Value", new int[0]);
            this.errorHandler = this.secsDriver.Reply(this.driverName, transaction, sEComData.HeaderItems.SystemBytes);
            if (this.errorHandler != SEComError.SEComPlugIn.ERR_NONE)
            {
                string text = this.errorHandler.ToString();
            }
        }
        private void ReplyS5F5(SXTransaction transaction, SecsValue secsValue)
        {
            SEComData sEComData = (SEComData)secsValue.SecsData;
            transaction.WriteNode(SX.SECSFormat.L, 1, "", "", new int[0]);
            transaction.WriteNode(SX.SECSFormat.L, 3, "", "", new int[0]);
            transaction.WriteNode(SX.SECSFormat.B, 1, secsValue.GetRequestValue("ALARMCODE"), "Alarm Code", new int[0]);
            transaction.WriteNode(SX.SECSFormat.U4, 1, secsValue.GetRequestValue("ALARMID"), "Alarm ID", new int[0]);
            transaction.WriteNode(SX.SECSFormat.A, 2, secsValue.GetRequestValue("ALARMTEXT"), "Alarm Text", new int[0]);
            this.errorHandler = this.secsDriver.Reply(this.driverName, transaction, sEComData.HeaderItems.SystemBytes);
            if (this.errorHandler != SEComError.SEComPlugIn.ERR_NONE)
            {
                string text = this.errorHandler.ToString();
            }
        }
        private void ReplyS5F3(SXTransaction transaction, SecsValue secsValue)
        {
            SEComData sEComData = (SEComData)secsValue.SecsData;
            transaction.WriteNode(SX.SECSFormat.B, 1, secsValue.GetRequestValue("ACK"), "ACK", new int[0]);
            this.errorHandler = this.secsDriver.Reply(this.driverName, transaction, sEComData.HeaderItems.SystemBytes);
            if (this.errorHandler != SEComError.SEComPlugIn.ERR_NONE)
            {
                string text = this.errorHandler.ToString();
            }
        }
        private void ReplyS2F49(SXTransaction transaction, SecsValue secsValue)
        {
            SEComData sEComData = (SEComData)secsValue.SecsData;
            this.errorHandler = this.secsDriver.Reply(this.driverName, transaction, sEComData.HeaderItems.SystemBytes);
            if (this.errorHandler != SEComError.SEComPlugIn.ERR_NONE)
            {
                string text = this.errorHandler.ToString();
            }
        }
        private void ReplyS2F41(SXTransaction transaction, SecsValue secsValue)
        {
            SEComData sEComData = (SEComData)secsValue.SecsData;
            this.errorHandler = this.secsDriver.Reply(this.driverName, transaction, sEComData.HeaderItems.SystemBytes);
            if (this.errorHandler != SEComError.SEComPlugIn.ERR_NONE)
            {
                string text = this.errorHandler.ToString();
            }
        }
        private void ReplyS2F33(SXTransaction transaction, SecsValue secsValue)
        {
            SEComData sEComData = (SEComData)secsValue.SecsData;
            transaction.WriteNode(SX.SECSFormat.B, 1, secsValue.GetRequestValue("ACK"), "ACK", new int[0]);
            this.errorHandler = this.secsDriver.Reply(this.driverName, transaction, sEComData.HeaderItems.SystemBytes);
            if (this.errorHandler != SEComError.SEComPlugIn.ERR_NONE)
            {
                string text = this.errorHandler.ToString();
            }
        }
        private void ReplyS2F31(SXTransaction transaction, SecsValue secsValue)
        {
            SEComData sEComData = (SEComData)secsValue.SecsData;
            this.errorHandler = this.secsDriver.Reply(this.driverName, transaction, sEComData.HeaderItems.SystemBytes);
            if (this.errorHandler != SEComError.SEComPlugIn.ERR_NONE)
            {
                string text = this.errorHandler.ToString();
            }
        }
        private void ReplyS2F17(SXTransaction transaction, SecsValue secsValue)
        {
            SEComData sEComData = (SEComData)secsValue.SecsData;
            this.errorHandler = this.secsDriver.Reply(this.driverName, transaction, sEComData.HeaderItems.SystemBytes);
            if (this.errorHandler != SEComError.SEComPlugIn.ERR_NONE)
            {
                string text = this.errorHandler.ToString();
            }
        }
        private void ReplyS2F15(SXTransaction transaction, SecsValue secsValue)
        {
            SEComData sEComData = (SEComData)secsValue.SecsData;
            int num = int.Parse(secsValue.GetValue("ECIDCOUNT"));
            transaction.WriteNode(SX.SECSFormat.B, 1, "0", "", new int[0]);
            this.errorHandler = this.secsDriver.Reply(this.driverName, transaction, sEComData.HeaderItems.SystemBytes);
            if (this.errorHandler != SEComError.SEComPlugIn.ERR_NONE)
            {
                string text = this.errorHandler.ToString();
            }
        }
        private void ReplyS2F13(SXTransaction transaction, SecsValue secsValue)
        {
            SEComData sEComData = (SEComData)secsValue.SecsData;
            this.errorHandler = this.secsDriver.Reply(this.driverName, transaction, sEComData.HeaderItems.SystemBytes);
            if (this.errorHandler != SEComError.SEComPlugIn.ERR_NONE)
            {
                string text = this.errorHandler.ToString();
            }
        }
        private void ReplyS1F17(SXTransaction transaction, SecsValue secsValue)
        {
            SEComData sEComData = (SEComData)secsValue.SecsData;
            this.errorHandler = this.secsDriver.Reply(this.driverName, transaction, sEComData.HeaderItems.SystemBytes);
            if (this.errorHandler != SEComError.SEComPlugIn.ERR_NONE)
            {
                string text = this.errorHandler.ToString();
            }
        }
        private void ReplyS1F15(SXTransaction transaction, SecsValue secsValue)
        {
            SEComData sEComData = (SEComData)secsValue.SecsData;
            this.errorHandler = this.secsDriver.Reply(this.driverName, transaction, sEComData.HeaderItems.SystemBytes);
            if (this.errorHandler != SEComError.SEComPlugIn.ERR_NONE)
            {
                string text = this.errorHandler.ToString();
            }
        }
        private void ReplyS1F3(SXTransaction transaction, SecsValue secsValue)
        {
            SEComData sEComData = (SEComData)secsValue.SecsData;
            this.ReplyS1F3GenerateMessage(transaction, secsValue);
            this.errorHandler = this.secsDriver.Reply(this.driverName, transaction, sEComData.HeaderItems.SystemBytes);
            if (this.errorHandler != SEComError.SEComPlugIn.ERR_NONE)
            {
                string text = this.errorHandler.ToString();
            }
        }
        private void ReplyS1F3GenerateMessage(SXTransaction transaction, SecsValue secsValue)
        {
        }
        #endregion

        #region "构造函数及初始化"
        public SecomDriver()
        {
        }
        /// <summary>
        /// 初始化sece驱动
        /// PASSIVE 模式
        /// </summary>
        /// <param name="driverName"></param>
        /// <param name="prop"></param>
        public override void Initialize(string driverName, SecsProperty prop)
        {
            this.driverName = driverName;
/*            //创建CSECom对象
            this.secsDriver = new CSEComPlugIn();
            this.errorHandler = this.secsDriver.Initialize(driverName, true, true);
            SXProFile sxProfile = this.secsDriver.GetProFile(driverName);
            if (this.errorHandler != SEComError.SEComPlugIn.ERR_NONE)
            {
                string strError = this.errorHandler.ToString();
            }
            //添加驱动事件
            this.AddDelegate();*/
            //加载ceid/vid配置
            //this.ReadDynamicReportValue();
            this.LoadMsgCEIDDicts();
            this.LoadMsgVIDDicts();


            //初始化连接配置
            SXProFile m_ProFile = new SXProFile();
            string hostLogPath = AppDomain.CurrentDomain.BaseDirectory + "hostLog";
            m_ProFile.Add(SXProFile.ProFileKey.KEY_COMMON_EQUIPMENTID, "LFTC");
            m_ProFile.Add(SXProFile.ProFileKey.KEY_COMMON_DRVVERSION, "2.1.1.15");
            m_ProFile.Add(SXProFile.ProFileKey.KEY_COMMON_IDENTITY, "EQP");
            m_ProFile.Add(SXProFile.ProFileKey.KEY_COMMON_DEVICEID, "1");
            m_ProFile.Add(SXProFile.ProFileKey.KEY_COMMON_SECSMODE, "HSMS");
            m_ProFile.Add(SXProFile.ProFileKey.KEY_HSMS_HSMSMODE, "PASSIVE");//ACTIVE:主动连接；PASSIVE:监听连接
            m_ProFile.Add(SXProFile.ProFileKey.KEY_HSMS_LOCALPORT, "5000");
            //m_ProFile.Add(SXProFile.ProFileKey.KEY_HSMS_REMOTEPORT, "5001");
            //m_ProFile.Add(SXProFile.ProFileKey.KEY_HSMS_REMOTEIP, "127.0.0.1");
            //m_ProFile.Add(SXProFile.ProFileKey.KEY_LOG_DIR, @"D:\\LOG");
            m_ProFile.Add(SXProFile.ProFileKey.KEY_LOG_DIR, hostLogPath);
            m_ProFile.Add(SXProFile.ProFileKey.KEY_LOG_DRIVERMODE, "H");
            m_ProFile.Add(SXProFile.ProFileKey.KEY_LOG_SECSIMODE, "H");
            m_ProFile.Add(SXProFile.ProFileKey.KEY_LOG_SECSIIMODE, "H");
            m_ProFile.Add(SXProFile.ProFileKey.KEY_LOG_DRIVERLEVEL, "5");
            m_ProFile.Add(SXProFile.ProFileKey.KEY_LOG_BACKUP, "28");
            //Secom配置文件
            m_ProFile.Add(SXProFile.ProFileKey.KEY_COMMON_USEDISPATCHER, "FALSE");
            m_ProFile.Add(SXProFile.ProFileKey.KEY_COMMON_SMDCLASS, "./SecomSMD/LFTC_Secom.SMD");

            if (this.secsDriver == null)
            {
                this.secsDriver = new CSEComPlugIn();
                this.AddDelegate();
            }
            //this.errorHandler = this.secsDriver.Initialize(this.driverName, true, false);
            this.errorHandler = this.secsDriver.Initialize(this.driverName, true, true, false, true, true, m_ProFile);
            if (this.errorHandler != SEComError.SEComPlugIn.ERR_NONE)
            {
                string text = this.errorHandler.ToString();
            }
        }
        public override void PassiveOpen()
        {
            //初始化连接配置
            SXProFile m_ProFile = new SXProFile();
            this.AddDelegate();
            string hostLogPath = AppDomain.CurrentDomain.BaseDirectory + "hostLog";
            m_ProFile.Add(SXProFile.ProFileKey.KEY_COMMON_EQUIPMENTID, "LFTC");
            m_ProFile.Add(SXProFile.ProFileKey.KEY_COMMON_DRVVERSION, "2.1.1.15");
            m_ProFile.Add(SXProFile.ProFileKey.KEY_COMMON_IDENTITY, "EQP");
            m_ProFile.Add(SXProFile.ProFileKey.KEY_COMMON_DEVICEID, "1");
            m_ProFile.Add(SXProFile.ProFileKey.KEY_COMMON_SECSMODE, "HSMS");
            m_ProFile.Add(SXProFile.ProFileKey.KEY_HSMS_HSMSMODE, "PASSIVE");//ACTIVE:主动连接；PASSIVE:监听连接
            m_ProFile.Add(SXProFile.ProFileKey.KEY_HSMS_LOCALPORT, "5000");
            //m_ProFile.Add(SXProFile.ProFileKey.KEY_HSMS_REMOTEPORT, "5001");
            //m_ProFile.Add(SXProFile.ProFileKey.KEY_HSMS_REMOTEIP, "127.0.0.1");


            //m_ProFile.Add(SXProFile.ProFileKey.KEY_LOG_DIR, @"D:\\LOG");
            m_ProFile.Add(SXProFile.ProFileKey.KEY_LOG_DIR, hostLogPath);
            m_ProFile.Add(SXProFile.ProFileKey.KEY_LOG_DRIVERMODE, "H");
            m_ProFile.Add(SXProFile.ProFileKey.KEY_LOG_SECSIMODE, "H");
            m_ProFile.Add(SXProFile.ProFileKey.KEY_LOG_SECSIIMODE, "H");
            m_ProFile.Add(SXProFile.ProFileKey.KEY_LOG_DRIVERLEVEL, "5");
            m_ProFile.Add(SXProFile.ProFileKey.KEY_LOG_BACKUP, "28");
            //Secom配置文件
            m_ProFile.Add(SXProFile.ProFileKey.KEY_COMMON_USEDISPATCHER, "FALSE");
            m_ProFile.Add(SXProFile.ProFileKey.KEY_COMMON_SMDCLASS, "./SecomSMD/LFTC_Secom.SMD");

            if (this.secsDriver == null)
            {
                this.secsDriver = new CSEComPlugIn();
                this.AddDelegate();
            }
            //this.errorHandler = this.secsDriver.Initialize(this.driverName, true, false);
            this.errorHandler = this.secsDriver.Initialize(this.driverName, true, true, false, true, true, m_ProFile);
            if (this.errorHandler != SEComError.SEComPlugIn.ERR_NONE)
            {
                string text = this.errorHandler.ToString();
            }
        }
        /// <summary>
        /// 通过配置，打开HsmsConnection连接
        /// ACTIVE 模式
        /// </summary>
        public override void Open()
        {

            SXProFile m_ProFile = new SXProFile();
            string hostLogPath = AppDomain.CurrentDomain.BaseDirectory + "hostLog";
            m_ProFile.Add(SXProFile.ProFileKey.KEY_COMMON_EQUIPMENTID, "LFTC");
            m_ProFile.Add(SXProFile.ProFileKey.KEY_COMMON_DRVVERSION, "2.1.1.15");
            m_ProFile.Add(SXProFile.ProFileKey.KEY_COMMON_IDENTITY, "EQP");
            m_ProFile.Add(SXProFile.ProFileKey.KEY_COMMON_DEVICEID, "1");
            m_ProFile.Add(SXProFile.ProFileKey.KEY_COMMON_SECSMODE, "HSMS");
            m_ProFile.Add(SXProFile.ProFileKey.KEY_HSMS_HSMSMODE, "ACTIVE");//ACTIVE:主动连接；PASSIVE:监听连接
            //m_ProFile.Add(SXProFile.ProFileKey.KEY_HSMS_LOCALPORT, "5000");
            m_ProFile.Add(SXProFile.ProFileKey.KEY_HSMS_REMOTEPORT, "5000");
            m_ProFile.Add(SXProFile.ProFileKey.KEY_HSMS_REMOTEIP, "127.0.0.1");
            //m_ProFile.Add(SXProFile.ProFileKey.KEY_LOG_DIR, @"D:\\LOG");
            m_ProFile.Add(SXProFile.ProFileKey.KEY_LOG_DIR, hostLogPath);
            m_ProFile.Add(SXProFile.ProFileKey.KEY_LOG_DRIVERMODE, "H");
            m_ProFile.Add(SXProFile.ProFileKey.KEY_LOG_SECSIMODE, "H");
            m_ProFile.Add(SXProFile.ProFileKey.KEY_LOG_SECSIIMODE, "H");
            m_ProFile.Add(SXProFile.ProFileKey.KEY_LOG_DRIVERLEVEL, "5");
            m_ProFile.Add(SXProFile.ProFileKey.KEY_LOG_BACKUP, "28");
            //Secom配置文件
            m_ProFile.Add(SXProFile.ProFileKey.KEY_COMMON_SMDCLASS, "./SecomSMD/LFTC_Secom.smd");

            if (this.secsDriver == null)
            {
                this.secsDriver = new CSEComPlugIn();
                this.AddDelegate();
            }
            //this.errorHandler = this.secsDriver.Initialize(this.driverName, true, false);
            this.errorHandler = this.secsDriver.Initialize(this.driverName, true, true, false, true, true, m_ProFile);
            if (this.errorHandler != SEComError.SEComPlugIn.ERR_NONE)
            {
                string text = this.errorHandler.ToString();
            }
        }
        public override void Reopen()
        {
            this.Close();
            this.Open();
        }
        public override void Close()
        {
            if (this.secsDriver != null)
            {
                this.secsDriver.Terminate(this.driverName);
                this.RemoveDelegate();
            }
        }
        #endregion

        public override void SendMessage(string sxfx, string param, object obj1)
        {
            this.SendMessage(sxfx, param, new object[]
			{
				obj1
			});
        }
        public override void SendMessage(string sxfx, string param, object obj1, object obj2)
        {
            this.SendMessage(sxfx, param, new object[]
			{
				obj1,
				obj2
			});
        }
        public override void SendMessage(string sxfx, string param, object obj1, object obj2, object obj3)
        {
            this.SendMessage(sxfx, param, new object[]
			{
				obj1,
				obj2,
				obj3
			});
        }
        public override void SendMessage(string sxfx, string param, object obj1, object obj2, object obj3, object obj4)
        {
            this.SendMessage(sxfx, param, new object[]
			{
				obj1,
				obj2,
				obj3,
				obj4
			});
        }
        public override void SendMessage(string sxfx, string param, object obj1, object obj2, object obj3, object obj4, object obj5)
        {
            this.SendMessage(sxfx, param, new object[]
			{
				obj1,
				obj2,
				obj3,
				obj4,
				obj5
			});
        }
        public override void SendMessage(string sxfx, object obj1)
        {
            this.SendMessage(sxfx, null, new object[]
			{
				obj1
			});
        }
        public override void SendMessage(string sxfx, object obj1, object obj2)
        {
            this.SendMessage(sxfx, null, new object[]
			{
				obj1,
				obj2
			});
        }
        public override void SendMessage(string sxfx, object obj1, object obj2, object obj3)
        {
            this.SendMessage(sxfx, null, new object[]
			{
				obj1,
				obj2,
				obj3
			});
        }
        public override void SendMessage(string sxfx, object obj1, object obj2, object obj3, object obj4)
        {
            this.SendMessage(sxfx, null, new object[]
			{
				obj1,
				obj2,
				obj3,
				obj4
			});
        }
        public override void SendMessage(string sxfx, object obj1, object obj2, object obj3, object obj4, object obj5)
        {
            this.SendMessage(sxfx, null, new object[]
			{
				obj1,
				obj2,
				obj3,
				obj4,
				obj5
			});
        }
        public override void SendMessage(string sxfx, object[] objs)
        {
            this.SendMessage(sxfx, null, objs);
        }
        public override void SendMessage(string sxfx, string param, object[] values)
        {
            if (LifterMainForm.PlaybackMode)
            {
                return;
            }
            if (param != null)
            {
                if (this.LftSem.HostControlState != enumHostControlState.SC_ONLINE_HOST)
                {
                    if (sxfx == "S6F11" && param == "3")
                    {
                        this.SendS6F11(values, param);
                        return;
                    }
                    if (sxfx == "S6F11" && param == "1")
                    {
                        this.SendS6F11(values, param);
                        return;
                    }
                    return;
                }
            }
            switch (sxfx)
            {
                case "S1F1":
                    this.SendS1F1();
                    break;
                case "S10F1":
                    this.SendS10F1();
                    break;
                case "S1F13":
                    this.SendS1F13("MDLN", "SOFTVER");
                    break;
                case "S2F17":
                    this.SendS2F17();
                    break;
                case "S5F1":
                    this.SendS5F1(param, (EcpAlarm)values[0]);
                    break;
                case "S6F11":
                    this.SendS6F11(values, param);
                    break;
                case "S6F13":
                    this.SendS6F13();
                    break;
            }
        }
        public override void ReplyMessage(string sxfx, object secsTransaction, SecsValue secsValue)
        {
            SXTransaction transaction = (SXTransaction)secsTransaction;
            switch (sxfx)
            {
                case "S1F1":
                    this.ReplyS1F1(transaction, secsValue);
                    return;
                case "S1F3":
                    this.ReplyS1F3(transaction, secsValue);
                    return;
                case "S1F13":
                    this.ReplyS1F13(transaction, secsValue);
                    return;
                case "S1F15":
                    this.ReplyS1F15(transaction, secsValue);
                    return;
                case "S1F17":
                    this.ReplyS1F17(transaction, secsValue);
                    return;
                case "S2F13":
                    this.ReplyS2F13(transaction, secsValue);
                    return;
                case "S2F15":
                    this.ReplyS2F15(transaction, secsValue);
                    return;
                case "S2F17":
                    this.ReplyS2F17(transaction, secsValue);
                    return;
                case "S2F31":
                    this.ReplyS2F31(transaction, secsValue);
                    return;
                case "S2F33":
                    this.ReplyS2F33(transaction, secsValue);
                    return;
                case "S2F41":
                    this.ReplyS2F41(transaction, secsValue);
                    return;
                case "S2F49":
                    this.ReplyS2F49(transaction, secsValue);
                    return;
                case "S5F3":
                    this.ReplyS5F3(transaction, secsValue);
                    return;
                case "S5F5":
                    this.ReplyS5F5(transaction, secsValue);
                    return;
                case "S6F15":
                    this.ReplyS6F15(transaction, secsValue);
                    return;
                case "S10F3":
                    this.ReplyS10F3(transaction, secsValue);
                    return;
            }
            this.ReplyUnknowMessage(transaction, secsValue);
        }
        public override void TerminateSEComDriver()
        {
            this.secsDriver.Terminate(this.driverName);
        }
        /// <summary>
        /// 为驱动基本委托事件添加方法
        /// </summary>
        private void AddDelegate()
        {
            try
            {
                this.secsDriver.OnSECS1Log += new CSEComPlugIn.DLGSECS1Log(this.SecomOnSECS1Log);
                this.secsDriver.OnSECS2Log += new CSEComPlugIn.DLGSECS2Log(this.SecomOnSECS2Log);
                this.secsDriver.OnSECSAbortMessage += new CSEComPlugIn.DLGSECSAbortMessage(this.SecomOnSECSAbortMessage);
                this.secsDriver.OnSECSConnected += new CSEComPlugIn.DLGSECSConnected(this.SecomOnSECSConnected);
                this.secsDriver.OnSECSDisConnected += new CSEComPlugIn.DLGSECSDisConnected(this.SecomOnSECSDisConnected);
                this.secsDriver.OnSECSInvalidReceived += new CSEComPlugIn.DLGSECSInvalidReceived(this.SecomOnSECSInvalidReceived);
                this.secsDriver.OnSECSReceived += new CSEComPlugIn.DLGSECSReceived(this.OnSECSReceived);
                this.secsDriver.OnSECSTimeOut += new CSEComPlugIn.DLGSECSTimeOut(this.OnSECSTimeOut);
                this.secsDriver.OnSECSUnknownMessage += new CSEComPlugIn.DLGSECSUnknownMessage(this.OnSECSUnknownMessage);
            }
            catch (Exception)
            {
            }
        }
        public void RemoveDelegate()
        {
            this.secsDriver.OnSECS1Log -= new CSEComPlugIn.DLGSECS1Log(this.SecomOnSECS1Log);
            this.secsDriver.OnSECS2Log -= new CSEComPlugIn.DLGSECS2Log(this.SecomOnSECS2Log);
            this.secsDriver.OnSECSAbortMessage -= new CSEComPlugIn.DLGSECSAbortMessage(this.SecomOnSECSAbortMessage);
            this.secsDriver.OnSECSConnected -= new CSEComPlugIn.DLGSECSConnected(this.SecomOnSECSConnected);
            this.secsDriver.OnSECSDisConnected -= new CSEComPlugIn.DLGSECSDisConnected(this.SecomOnSECSDisConnected);
            this.secsDriver.OnSECSInvalidReceived -= new CSEComPlugIn.DLGSECSInvalidReceived(this.SecomOnSECSInvalidReceived);
            this.secsDriver.OnSECSReceived -= new CSEComPlugIn.DLGSECSReceived(this.OnSECSReceived);
            this.secsDriver.OnSECSTimeOut -= new CSEComPlugIn.DLGSECSTimeOut(this.OnSECSTimeOut);
            this.secsDriver.OnSECSUnknownMessage -= new CSEComPlugIn.DLGSECSUnknownMessage(this.OnSECSUnknownMessage);
        }
        private void OnSECSUnknownMessage(string aEquipmentID, XmlDocument aXML)
        {
            if (this.OnUnknownMessage != null)
            {
                this.OnUnknownMessage(this, new SecsEventArgs("UnknownMessage", "", aXML.OuterXml.ToString()));
            }
        }
        private void OnSECSTimeOut(string aEquipmentID, XmlDocument aXML, string aTimeOut)
        {
            if (this.OnTimeoutOccured != null)
            {
                string messageBody;
                if (aTimeOut != null)
                {
                    if (aTimeOut == "1003")
                    {
                        messageBody = "T3";
                    }
                    else if (aTimeOut == "1005")
                    {
                        messageBody = "T5";
                    }
                    else if (aTimeOut == "1006")
                    {
                        messageBody = "T6";
                    }
                    else if (aTimeOut == "1007")
                    {
                        messageBody = "T7";
                    }
                    else if (aTimeOut == "1008")
                    {
                        messageBody = "T8";
                    }
                    else
                    {
                        messageBody = "SECS1 Timeout";
                    }
                }
                else
                {
                    messageBody = "SECS1 Timeout";
                }
                this.OnTimeoutOccured(this, new SecsEventArgs("", "", messageBody, aTimeOut, aXML.OuterXml.ToString()));
            }
        }
        private void SecomOnSECSInvalidReceived(string aEquipmentID, XmlDocument aXML)
        {
            if (this.OnInvalidMessage != null)
            {
                this.OnInvalidMessage(this, new SecsEventArgs("InvalidMessage", "", aXML.OuterXml.ToString()));
            }
        }
        private void SecomOnSECSDisConnected(string aEquipmentID, XmlDocument aXML)
        {
            if (this.OnDisconnected != null)
            {
                base.IsConnected = false;
                this.OnDisconnected(this, new SecsEventArgs("", "", "Disconnected"));
                this.LftSem.HostControlStatePublish();
                LogHelper.loggerInfo("HOST断开连接");
            }
        }
        private void invokeSECSConneced(string aEquipmentID, XmlDocument aXML)
        {
            base.IsConnected = true;
        }
        private void SecomOnSECSConnected(string aEquipmentID, XmlDocument aXML)
        {
            if (this.OnConnected != null)
            {
                base.IsConnected = true;
                this.OnConnected(this, new SecsEventArgs("", "", "Connected"));
                this.LftSem.HostControlStatePublish();
                LogHelper.loggerInfo("HOST连接成功");
            }
        }
        private void SecomOnSECSAbortMessage(string aEquipmentID, XmlDocument aXML)
        {
            if (this.OnAbortMessage != null)
            {
                this.OnAbortMessage(this, new SecsEventArgs("", "", "AbortMessage", "", aXML.OuterXml.ToString()));
            }
        }
        private void SecomOnSECS2Log(string aEquipmentID, string aDirection, string aSECS2)
        {
            if (this.OnSecs2Log != null)
            {
                this.OnSecs2Log(this, new SecsEventArgs("", "", aDirection, aSECS2));
            }
        }
        private void SecomOnSECS1Log(string aEquipmentID, string aDirection, string aSECS1)
        {
            if (this.OnSecs1Log != null)
            {
                this.OnSecs1Log(this, new SecsEventArgs("", "", aDirection, aSECS1));
            }
        }
        private void OnSECSReceived(string aEquipmentID, string aSECSMsgName, XmlDocument aXML)
        {
            SEComData sEComData = new SEComData();
            sEComData.XmlToSECSData(aXML);
            string strSxFx = string.Concat(new object[]
			{
				"S",
				sEComData.HeaderItems.Stream,
				"F",
				sEComData.HeaderItems.Function
			});
            switch (strSxFx)
            {
                case "S1F1":
                    this.ReceiveS1F1(sEComData);
                    return;
                case "S1F3":
                    this.ReceiveS1F3(sEComData);
                    return;
                case "S1F13":
                    this.ReceiveS1F13(sEComData);
                    return;
                case "S1F15":
                    this.ReceiveS1F15(sEComData);
                    return;
                case "S1F17":
                    this.ReceiveS1F17(sEComData);
                    return;
                case "S2F13":
                    this.ReceiveS2F13(sEComData);
                    return;
                case "S2F15":
                    this.ReceiveS2F15(sEComData);
                    return;
                case "S2F17":
                    this.ReceiveS2F17(sEComData);
                    return;
                case "S2F18":
                    this.ReceiveS2F18(sEComData);
                    return;
                case "S2F31":
                    this.ReceiveS2F31(sEComData);
                    return;
                case "S2F33":
                    this.ReceiveS2F33(sEComData);
                    return;
                case "S2F41":
                    this.ReceiveS2F41(sEComData);
                    return;
                case "S2F49":
                    this.ReceiveS2F49(sEComData);
                    return;
                case "S5F3":
                    this.ReceiveS5F3(sEComData);
                    return;
                case "S5F5":
                    this.ReceiveS5F5(sEComData);
                    return;
                case "S6F15":
                    this.ReceiveS6F15(sEComData);
                    return;
                case "S10F3":
                    this.ReceiveS10F3(sEComData);
                    return;
            }
            this.ReceiveUnknowMessage(sEComData);
        }
        /// <summary>
        /// 获取静态配置文件，初始化S6F11事件CEID数据
        /// </summary>
        private void ReadDynamicReportValue()
        {
            string str = "C:\\ecp\\Lifter\\INI\\";
            if (File.Exists(Application.StartupPath + "\\ini\\ceid.ini"))
            {
                str = Application.StartupPath + "\\ini\\";
            }
            TextReader textReader = new StreamReader(str + "CEID.ini");
            TextReader textReader2 = new StreamReader(str + "RPT.ini");
            TextReader textReader3 = new StreamReader(str + "VID.ini");
            TextReader textReader4 = new StreamReader(str + "VIDVAL.ini");
            int num = 0;
            while (true)
            {
                string text = textReader.ReadLine();
                if (text == null)
                {
                    break;
                }
                this.ceidList[num] = text;
                num++;
            }
            num = 0;
            while (true)
            {
                string text = textReader2.ReadLine();
                if (text == null)
                {
                    break;
                }
                this.reportList[num] = text;
                num++;
            }
            num = 0;
            while (true)
            {
                string text = textReader3.ReadLine();
                if (text == null)
                {
                    break;
                }
                this.vidList[num] = text;
                num++;
            }
            num = 0;
            while (true)
            {
                string text = textReader4.ReadLine();
                if (text == null)
                {
                    break;
                }
                this.vidMapper[num] = text;
                num++;
            }
        }
        /// <summary>
        /// 从数据库CEID表加载所有的CEID配置
        /// </summary>
        /// <returns></returns>
        private void  LoadMsgCEIDDicts()
        {
            DBConnection dBConnection = DBControl.GetConnection();
            string sqlString = "SELECT * FROM HSMS_CEID";
            SqlDataReader sqlDataReader = dBConnection.ExecuteQuery(sqlString);
            while (sqlDataReader.Read())
            {
                try
                {
                    MsgCEID msgCEID = new MsgCEID();
                    msgCEID.Ceid = Convert.ToInt32(sqlDataReader["CEID"]);
                    msgCEID.EventName = sqlDataReader["EVENTNAME"].ToString();
                    msgCEID.ReportID = Convert.ToInt32(sqlDataReader["REPORTID"]);
                    msgCEID.Vid = sqlDataReader["VID"].ToString();
                    msgCEID.Use = sqlDataReader["USE"].ToString();
                    this.msgCeidDict.Add(msgCEID.Ceid, msgCEID);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            if (sqlDataReader != null)
            {
                sqlDataReader.Close();
            }
            DBControl.RemoveDBConection(dBConnection);
        }
        /// <summary>
        /// 从数据库VID表加载所有的VID配置
        /// </summary>
        private void LoadMsgVIDDicts()
        {
            DBConnection dBConnection = DBControl.GetConnection();
            string sqlString = "SELECT * FROM HSMS_VID";
            SqlDataReader sqlDataReader = dBConnection.ExecuteQuery(sqlString);
            while (sqlDataReader.Read())
            {
                try
                {
                    MsgVID msgVID = new MsgVID();
                    msgVID.Vid = Convert.ToInt32(sqlDataReader["VID"]);
                    msgVID.HostName = sqlDataReader["HOSTNAME"].ToString();
                    msgVID.LocalName = sqlDataReader["LOCALNAME"].ToString();
                    msgVID.Type = sqlDataReader["TYPE"].ToString();
                    msgVID.Length = Convert.ToInt32(sqlDataReader["LENGTH"]);
                    msgVID.VidNode = sqlDataReader["VIDNODE"].ToString();
                    msgVidDict.Add(msgVID.Vid,msgVID);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
            if (sqlDataReader != null)
            {
                sqlDataReader.Close();
            }
            DBControl.RemoveDBConection(dBConnection);
        }
        private string GetValue(object obj, string key)
        {
            Type type = obj.GetType();
            return type.GetProperty(key).GetValue(obj, null).ToString();
        }

        #region "SendMessage"
        private void SendS1F1()
        {
            SXTransaction sXTransaction = S1F1.MakeTransactionSend();

            this.errorHandler = this.secsDriver.Request(this.driverName, sXTransaction);
            if (this.errorHandler != SEComError.SEComPlugIn.ERR_NONE)
            {
                string text = this.errorHandler.ToString();
            }
        }
        private void SendS1F13(string mdln, string softver)
        {
            SXTransaction sXTransaction = new SXTransaction();
            sXTransaction.Stream = 1;
            sXTransaction.Function = 13;
            sXTransaction.Wait = true;
            sXTransaction.MessageData = "Establish Communications Request.";
            sXTransaction.WriteNode(SX.SECSFormat.L, 2, "", "", new int[0]);
            sXTransaction.WriteNode(SX.SECSFormat.A, 0, mdln, "MDLN", new int[0]);
            sXTransaction.WriteNode(SX.SECSFormat.A, 0, softver, "SOFTVER", new int[0]);
            this.errorHandler = this.secsDriver.Request(this.driverName, sXTransaction);
            if (this.errorHandler != SEComError.SEComPlugIn.ERR_NONE)
            {
                string text = this.errorHandler.ToString();
            }
        }
        private void SendS2F17()
        {
            SXTransaction sXTransaction = new SXTransaction();
            sXTransaction.Stream = 2;
            sXTransaction.Function = 17;
            sXTransaction.Wait = true;
            sXTransaction.MessageData = "DateTimeRequest";
            this.errorHandler = this.secsDriver.Request(this.driverName, sXTransaction);
            if (this.errorHandler != SEComError.SEComPlugIn.ERR_NONE)
            {
                string text = this.errorHandler.ToString();
            }
        }
        private void SendS5F1(string alarmCode, EcpAlarm alarm)
        {
            SXTransaction sXTransaction = S5F1.MakeTransactionSend(alarm);

            this.errorHandler = this.secsDriver.Request(this.driverName, sXTransaction);
            if (this.errorHandler != SEComError.SEComPlugIn.ERR_NONE)
            {
                string text = this.errorHandler.ToString();
            }
        }
        private void SendS6F13()
        {
            SXTransaction sXTransaction = new SXTransaction();
            sXTransaction.Stream = 6;
            sXTransaction.Function = 13;
            sXTransaction.Wait = true;
            sXTransaction.MessageData = "Annotated Event Report Send";
            sXTransaction.WriteNode(SX.SECSFormat.L, 3, "", "", new int[0]);
            sXTransaction.WriteNode(SX.SECSFormat.U2, 0, "", "DataID", new int[0]);
            sXTransaction.WriteNode(SX.SECSFormat.U2, 0, "", "CEID", new int[0]);
            sXTransaction.WriteNode(SX.SECSFormat.L, 1, "", "", new int[0]);
            sXTransaction.WriteNode(SX.SECSFormat.L, 2, "", "", new int[0]);
            sXTransaction.WriteNode(SX.SECSFormat.U2, 0, "", "ReportID", new int[0]);
            sXTransaction.WriteNode(SX.SECSFormat.L, 1, "", "", new int[0]);
            sXTransaction.WriteNode(SX.SECSFormat.L, 2, "", "", new int[0]);
            sXTransaction.WriteNode(SX.SECSFormat.U2, 0, "", "VID", new int[0]);
            sXTransaction.WriteNode(SX.SECSFormat.A, 0, "", "Value", new int[0]);
            this.errorHandler = this.secsDriver.Request(this.driverName, sXTransaction);
            if (this.errorHandler != SEComError.SEComPlugIn.ERR_NONE)
            {
                string text = this.errorHandler.ToString();
            }
        }
        private void SendS10F1()
        {
            SXTransaction sXTransaction = new SXTransaction();
            sXTransaction.Stream = 10;
            sXTransaction.Function = 1;
            sXTransaction.Wait = true;
            sXTransaction.MessageData = "Terminal Request";
            sXTransaction.WriteNode(SX.SECSFormat.L, 2, "", "", new int[0]);
            sXTransaction.WriteNode(SX.SECSFormat.B, 0, "0", "Terminal ID", new int[0]);
            sXTransaction.WriteNode(SX.SECSFormat.A, 0, "请叫工作人员", "Text", new int[0]);
            this.errorHandler = this.secsDriver.Request(this.driverName, sXTransaction);
            if (this.errorHandler != SEComError.SEComPlugIn.ERR_NONE)
            {
                string text = this.errorHandler.ToString();
            }
        }
        private void SendS6F11(object[] objectList, string ceidValue)
        {
            int ceid = Convert.ToInt32(ceidValue);
            MsgCEID msgCEID = this.msgCeidDict[ceid];
            SXTransaction sXTransaction = S6F11.MakeTransactionSend(ceid);
            if (msgCeidDict.ContainsKey(msgCEID.Ceid))
            {
                sXTransaction.WriteNode(SX.SECSFormat.L, 3, "", "", new int[0]);

                sXTransaction.WriteNode(SX.SECSFormat.U4, 1, "0", "DATAID", new int[0]);
                sXTransaction.WriteNode(SX.SECSFormat.U2, 1, ceidValue, "CEID", new int[0]);
                if (msgCEID.ReportID == 0)
                {
                    sXTransaction.WriteNode(SX.SECSFormat.L, 0, "", "", new int[0]);
                }
                else
                {
                    //获取CEID对应的VID
                    sXTransaction.WriteNode(SX.SECSFormat.L, 1, "", "", new int[0]);
                    sXTransaction.WriteNode(SX.SECSFormat.L, 2, "", "", new int[0]);
                    sXTransaction.WriteNode(SX.SECSFormat.U2, 1, msgCEID.ReportID.ToString(), "REPORTID", new int[0]);
                    string[] CEID_VIDS = msgCEID.Vid.Split(',');
                    if (CEID_VIDS.Length > 0)
                    {
                        sXTransaction.WriteNode(SX.SECSFormat.L, CEID_VIDS.Length, "", "", new int[0]);

                        //获取vid值
                        Dictionary<string, string> vidValues = this.GetVidValues(objectList);
                        this.BuildS6F11CEID(ref sXTransaction, vidValues, CEID_VIDS);                    
                    }
                    else
                    {
                        sXTransaction.WriteNode(SX.SECSFormat.L, 0, "", "", new int[0]);
                    }
                }
            }
            else
            {
                //报警或记录Log日志（ceid在配置表中不存在）
                LogHelper.loggerInfo(string.Format("ceid[{0}]在配置表中不存在", msgCEID.Ceid));
            }
            this.errorHandler = this.secsDriver.Request(this.driverName, sXTransaction);
            if (this.errorHandler != SEComError.SEComPlugIn.ERR_NONE)
            {
                string text2 = this.errorHandler.ToString();
            }
        }
        private void BuildS6F11CEID(ref SXTransaction sXTransaction, Dictionary<string,string> vidValues, string[] vids)
        {

            //获取vid对应的vidNode
            foreach (string vid in vids)
            {
                //获取vid对象
                int vidKey = Convert.ToInt32(vid);
                if (msgVidDict.ContainsKey(vidKey))
                {
                    MsgVID msgVID = msgVidDict[vidKey];
                    //如果没有VIDNode项
                    if (string.IsNullOrEmpty(msgVID.VidNode))
                    {
                        SX.SECSFormat seceType = this.GetSECEFormat(msgVID.Type.Trim());
                        string value = "";
                        if (vidValues.ContainsKey(msgVID.LocalName))
                        {
                            value = vidValues[msgVID.LocalName];
                        }
                        sXTransaction.WriteNode(seceType, value.Length, value,value.ToUpper(),new int[0]);
                    }
                    else
                    {
                        //获取VID对应的vidNode集合
                        string[] vidArrays = msgVID.VidNode.Split(',');
                        sXTransaction.WriteNode(SX.SECSFormat.L, vidArrays.Length, "", "", new int[0]);
                        this.BuildS6F11CEID(ref sXTransaction, vidValues, vidArrays);
                    }
                }
            }
        }
        private Dictionary<string,string> GetVidValues(object[] units)
        {
            //判断传入的对象是否为多个,获取对应的VID值集合
            Dictionary<string, string> vidValues = new Dictionary<string, string>();
            if (units.Length > 1)
            {
                foreach (object unit in units)
                {
                    if (unit is LifterSEM)
                    {
                        LifterSEM lifterSEM = (LifterSEM)unit;
                        vidValues.Concat(lifterSEM.getVidValueForHost()).GroupBy(x => x.Key).ToDictionary(x => x.Key, x => x.First().Value);
                    }
                    else if (unit is EcpAlarm)
                    {
                        EcpAlarm ecpAlarm = (EcpAlarm)unit;
                        vidValues.Concat(ecpAlarm.getVidValueForHost()).GroupBy(x => x.Key).ToDictionary(x => x.Key, x => x.First().Value);
                    }
                    else if (unit is LifterPort)
                    {
                        LifterPort lifterPort = (LifterPort)unit;
                        if (vidValues != null && vidValues.Count > 0)
                        {
                            vidValues.Concat(lifterPort.getVidValueForHost()).GroupBy(x => x.Key).ToDictionary(x => x.Key, x => x.First().Value);
                        }
                        else
                        {
                            vidValues = lifterPort.getVidValueForHost();
                        }
                    }
                    else if (unit is ShuttleMaster)
                    {
                        ShuttleMaster shuttleMaster = (ShuttleMaster)unit;
                        vidValues.Concat(shuttleMaster.getVidValueForHost()).GroupBy(x => x.Key).ToDictionary(x => x.Key, x => x.First().Value);
                    }
                    else if (unit is Carrier)
                    {
                        Carrier carrier = (Carrier)unit;
                        if (vidValues != null && vidValues.Count > 0)
                        {
                            vidValues.Concat(carrier.getVidValueForHost()).GroupBy(x => x.Key).ToDictionary(x => x.Key, x => x.First().Value);
                        }
                        else
                        {
                            vidValues = carrier.getVidValueForHost();
                        }
                    }
                    else if (unit is TransferWorkItem)
                    {
                        TransferWorkItem transferWorkItem = (TransferWorkItem)unit;
                        //合并vid集合并去重
                        vidValues.Concat(transferWorkItem.getVidValueForHost()).GroupBy(x => x.Key).ToDictionary(x => x.Key, x => x.First().Value);
                    }
                }
            }
            else
            {
                object unit = units[0];
                if (unit is LifterSEM)
                {
                    LifterSEM lifterSEM = (LifterSEM)unit;
                    vidValues = lifterSEM.getVidValueForHost();
                }
                else if (unit is EcpAlarm)
                {
                    EcpAlarm ecpAlarm = (EcpAlarm)unit;
                    vidValues = ecpAlarm.getVidValueForHost();
                }
                else if (unit is LifterPort)
                {
                    LifterPort lifterPort = (LifterPort)unit;
                    vidValues = lifterPort.getVidValueForHost();
                }
                else if (unit is ShuttleMaster)
                {
                    ShuttleMaster shuttleMaster = (ShuttleMaster)unit;
                    vidValues = shuttleMaster.getVidValueForHost();
                }
                else if (unit is Carrier)
                {
                    Carrier carrier = (Carrier)unit;
                    vidValues = carrier.getVidValueForHost();
                }
                else if (unit is TransferWorkItem)
                {
                    TransferWorkItem transferWorkItem = (TransferWorkItem)unit;
                    vidValues = transferWorkItem.getVidValueForHost();
                }
                else if (unit is ConveyorBuffer)
                {
                    ConveyorBuffer conveyorBuffer = (ConveyorBuffer)unit;
                    vidValues = conveyorBuffer.getVidValueForHost();
                }
            }
            return vidValues;
        }
        private SX.SECSFormat GetSECEFormat(string format)
        {
            SX.SECSFormat aFormat = SX.SECSFormat.A;
            switch (format)
            {
                case "A":
                    aFormat = SX.SECSFormat.A;
                    break;
                case "BINARY":
                    aFormat = SX.SECSFormat.B;
                    break;
                case "BOOLEAN":
                    aFormat = SX.SECSFormat.BOOLEAN;
                    break;
                case "F4":
                    aFormat = SX.SECSFormat.F4;
                    break;
                case "F8":
                    aFormat = SX.SECSFormat.F8;
                    break;
                case "I1":
                    aFormat = SX.SECSFormat.I1;
                    break;
                case "I2":
                    aFormat = SX.SECSFormat.I2;
                    break;
                case "I4":
                    aFormat = SX.SECSFormat.I4;
                    break;
                case "I8":
                    aFormat = SX.SECSFormat.I8;
                    break;
                case "J":
                    aFormat = SX.SECSFormat.J;
                    break;
                case "L":
                    aFormat = SX.SECSFormat.L;
                    break;
                case "U1":
                    aFormat = SX.SECSFormat.U1;
                    break;
                case "U2":
                    aFormat = SX.SECSFormat.U2;
                    break;
                case "U4":
                    aFormat = SX.SECSFormat.U4;
                    break;
                case "U8":
                    aFormat = SX.SECSFormat.U8;
                    break;
                case "WA":
                    aFormat = SX.SECSFormat.WA;
                    break;
                case "X":
                    aFormat = SX.SECSFormat.X;
                    break;
            }
            return aFormat;
        }
/*        private void SendS6F11(object[] objectList, string ceidValue)
        {
            SXTransaction sXTransaction = new SXTransaction();
            sXTransaction.Stream = 6;
            sXTransaction.Function = 11;
            sXTransaction.MessageData = "S6F11";
            sXTransaction.Wait = true;
            Dictionary<string, string> vidDictionary = this.SetVID(objectList);
            string reportID = this.GetReportID(ceidValue);
            string text = this.GetReportList(reportID);
            sXTransaction.WriteNode(SX.SECSFormat.L, 3, "", "", new int[0]);
            sXTransaction.WriteNode(SX.SECSFormat.U4, 1, "0", "DATAID", new int[0]);
            string ceidText = SecsCEID.GetCeidText(ceidValue);
            sXTransaction.WriteNode(SX.SECSFormat.U2, 1, ceidValue, ceidText, new int[0]);
            if (reportID == null)
            {
                sXTransaction.WriteNode(SX.SECSFormat.L, 0, "", "", new int[0]);
            }
            else
            {
                if (!reportID.StartsWith("V:"))
                {
                    sXTransaction.WriteNode(SX.SECSFormat.L, 1, "", "", new int[0]);
                    sXTransaction.WriteNode(SX.SECSFormat.L, 2, "", "", new int[0]);
                    sXTransaction.WriteNode(SX.SECSFormat.U2, 1, reportID, "REPTID", new int[0]);
                    string[] array = text.Split(',');
                    sXTransaction.WriteNode(SX.SECSFormat.L, array.Length, "", "", new int[0]);
                    this.BuildVIDStructure(text, objectList, sXTransaction, vidDictionary);
                }
            }
            this.errorHandler = this.secsDriver.Request(this.driverName, sXTransaction);
            if (this.errorHandler != SEComError.SEComPlugIn.ERR_NONE)
            {
                string text2 = this.errorHandler.ToString();
            }
        }*/
        #endregion

        private void BuildVIDStructure(string vidValues, object[] objectList, SXTransaction transaction, Dictionary<string, string> vidDictionary)
        {
            string[] array = vidValues.Split(',');
            int i = 0;
            while (i < array.Length)
            {
                string text = array[i];
                if (text == null || (text != "LN" && text != "LV"))
                {
                    string vidStructure = this.GetVidStructure(array[i]);
                    if (vidStructure.Equals(array[i]))
                    {
                        string vIDInfo = this.GetVIDInfo(array[i]);
                        string[] array2 = vIDInfo.Split(',');
                        SX.SECSFormat aFormat = SX.SECSFormat.A;
                        if (array2 == null)
                        {
                            transaction.WriteNode(aFormat, 0, "UNKNOWN_VID", "UNKNOWN_VID", new int[0]);
                        }
                        else
                        {
                            string aItemName = array2[0];
                            string text2 = array2[1];
                            text = array2[1];
                            switch (text)
                            {
                                case "A":
                                    aFormat = SX.SECSFormat.A;
                                    break;
                                case "BINARY":
                                    aFormat = SX.SECSFormat.B;
                                    break;
                                case "BOOLEAN":
                                    aFormat = SX.SECSFormat.BOOLEAN;
                                    break;
                                case "F4":
                                    aFormat = SX.SECSFormat.F4;
                                    break;
                                case "F8":
                                    aFormat = SX.SECSFormat.F8;
                                    break;
                                case "I1":
                                    aFormat = SX.SECSFormat.I1;
                                    break;
                                case "I2":
                                    aFormat = SX.SECSFormat.I2;
                                    break;
                                case "I4":
                                    aFormat = SX.SECSFormat.I4;
                                    break;
                                case "I8":
                                    aFormat = SX.SECSFormat.I8;
                                    break;
                                case "J":
                                    aFormat = SX.SECSFormat.J;
                                    break;
                                case "L":
                                    aFormat = SX.SECSFormat.L;
                                    break;
                                case "U1":
                                    aFormat = SX.SECSFormat.U1;
                                    break;
                                case "U2":
                                    aFormat = SX.SECSFormat.U2;
                                    break;
                                case "U4":
                                    aFormat = SX.SECSFormat.U4;
                                    break;
                                case "U8":
                                    aFormat = SX.SECSFormat.U8;
                                    break;
                                case "WA":
                                    aFormat = SX.SECSFormat.WA;
                                    break;
                                case "X":
                                    aFormat = SX.SECSFormat.X;
                                    break;
                            }
                            string vidValueAtDictionary = this.GetVidValueAtDictionary(vidStructure, vidDictionary);
                            transaction.WriteNode(aFormat, 0, vidValueAtDictionary, aItemName, new int[0]);
                        }
                    }
                    else
                    {
                        this.BuildVIDStructure(vidStructure, objectList, transaction, vidDictionary);
                    }
                }
                else if (text == "LN")
                {
                    transaction.WriteNode(SX.SECSFormat.L, array.Length - i - 1, "", "", new int[0]);
                }
                else if (text == "LV")
                {
                    transaction.WriteNode(SX.SECSFormat.L, array.Length - 1, "", "", new int[0]);
                }
                i++;
            }
        }
        private string GetVIDInfo(string vidKey)
        {
            string result;
            for (int i = 0; i < this.vidMapper.Length; i++)
            {
                if (this.vidMapper[i] != null)
                {
                    string[] array = this.vidMapper[i].Split('=');
                    if (array.Length == 2)
                    {
                        if (array[0].Equals(vidKey))
                        {
                            result = array[1];
                            return result;
                        }
                    }
                }
            }
            result = "";
            return result;
        }

       
        private string GetVidStructure(string vidKey)
        {
            string result;
            for (int i = 0; i < this.vidList.Length; i++)
            {
                if (this.vidList[i] != null)
                {
                    string[] array = this.vidList[i].Split('=');
                    if (array.Length == 2)
                    {
                        if (array[0].Equals(vidKey))
                        {
                            string text = array[1];
                            if (text.Trim().Equals(""))
                            {
                                result = null;
                            }
                            else
                            {
                                result = text;
                            }
                            return result;
                        }
                    }
                }
            }
            result = null;
            return result;
        }
        private string GetVidValueAtDictionary(string vidKey, Dictionary<string, string> vidDictionary)
        {
            return vidDictionary[vidKey];
        }

        public override void Initialize(SecsProperty prop)
        {
        }
    }
}
